"""
Case Type   : show variables&show create database
Case Name   : like子句%匹配 多个通配符
Create At   : 2022/09/20
Owner       : zou_jialiang050
Description :
    1、create database
    2、show variables
    3、show session variables
    4、show global variables
    5、清理环境
Expect      :
    1、创建成功
    2、查询成功
    3、设置成功
    4、查询成功
    5、清理环境成功
History     :
    modified by @zou_jialiang050,2023.03.09，调整筛选条件规避结果数量变更问题
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class VariablesCase(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.commonsh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.constant = Constant()
        self.db_name = "db_var_0004"

    def test_variables(self):
        step1_text = "-----step1:create database;expect:创建成功-----"
        self.logger.info(step1_text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
                    create database {self.db_name} dbcompatibility = 'B';'''
        self.logger.info(sql_cmd1)
        sql_res1 = self.commonsh.execut_db_sql(sql_cmd1)
        self.logger.info(sql_res1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res1,
                      "创建数据库失败:" + step1_text)

        step2_text = "-----step2:show variables;expect:查询成功-----"
        self.logger.info(step2_text)
        sql_cmd2 = f"show variables like '%enable_memory_context%';"
        self.logger.info(sql_cmd2)
        sql_res2 = self.commonsh.execut_db_sql(sql_cmd2,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res2)
        self.assertIn("(1 row)", sql_res2, "查询失败：" + step2_text)

        step3_text = "-----step3:show global variables;expect:查询成功-----"
        self.logger.info(step3_text)
        sql_cmd3 = f"show global variables like '%enable_memory_context%';"
        self.logger.info(sql_cmd3)
        sql_res3 = self.commonsh.execut_db_sql(sql_cmd3,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res3)
        self.assertIn("(1 row)", sql_res3, "查询失败：" + step3_text)

        step4_text = "-----step4:show session variables;expect:查询成功-----"
        self.logger.info(step4_text)
        sql_cmd4 = f"show session variables like '%enable_memory_context%';"
        self.logger.info(sql_cmd4)
        sql_res4 = self.commonsh.execut_db_sql(sql_cmd4,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res4)
        self.assertIn("(1 row)", sql_res4, "查询失败：" + step4_text)

    def tearDown(self):
        step5_text = "-----step5:清理环境;expect:成功-----"
        self.logger.info(step5_text)

        drop_db_text = "-----drop database-----"
        self.logger.info(drop_db_text)
        drop_db_cmd = f'''drop database if exists {self.db_name};'''
        self.logger.info(drop_db_cmd)
        drop_db_res = self.commonsh.execut_db_sql(drop_db_cmd)
        self.logger.info(drop_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      drop_db_res, "drop database filed" + drop_db_res)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
